package com.tg.paw.common.mq.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.tg.paw.common.mq.service.IMqMessageLogService;
import com.tg.paw.common.mq.entity.MqMessageLog;
import com.tg.paw.common.annotation.SysLog;
import com.tg.paw.common.util.CommonResult;
import com.tg.paw.common.constant.AllEnum;
import com.tg.paw.common.util.NameUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>
 * 消息 前端控制器
 * </p>
 *
 * @author Rubble
 * @since 2020-06-19
 */
@RestController
@Slf4j
@Api(tags = "消息管理", value = "MqMessageLogController")
@RequestMapping("/mq/mqMessageLog")
public class MqMessageLogController {
    @Autowired
    private IMqMessageLogService mqMessageLogService;


    @SysLog(MODULE = "mq", REMARK = "根据条件查询所有消息列表")
    @ApiOperation("根据条件查询所有消息列表")
    @GetMapping(value = "/")
    public CommonResult getMqMessageLogByPage(MqMessageLog entity,
                                    @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                    @RequestParam(value = "sortField", defaultValue = "") String sortField,
                                    @RequestParam(value = "sortOrder", defaultValue = "") String sortOrder) {
        try {
            QueryWrapper<MqMessageLog> queryWrapper = new QueryWrapper<>(entity);
            if(StringUtils.isNotEmpty(sortField) && StringUtils.isNotEmpty(sortOrder)){
                sortField = NameUtil.humpToLine(sortField);
                if(AllEnum.SortEnum.ASC.desc().equals(sortOrder)){
                    queryWrapper.orderByAsc(sortField);
                }else {
                    queryWrapper.orderByDesc(sortField);
                }
            }
            return CommonResult.success(mqMessageLogService.page(new Page<>(pageNo, pageSize), queryWrapper));
        } catch (Exception e) {
            log.error("根据条件查询所有消息：{}", e.getMessage(), e);
        }
        return CommonResult.failed();
    }

    @SysLog(MODULE = "mq", REMARK = "保存消息")
    @ApiOperation("保存消息表")
    @PostMapping(value = "/")
    public CommonResult saveMqMessageLog(@RequestBody MqMessageLog entity) {
            try {
            if (mqMessageLogService.save(entity)) {
                return CommonResult.success();
            }
            } catch (Exception e) {
                log.error("保存消息表：{}", e.getMessage(), e);
                return CommonResult.failed();
            }
            return CommonResult.failed();
    }

    @SysLog(MODULE = "mq", REMARK = "更新消息")
    @ApiOperation("更新消息")
    @PutMapping(value = "/{id}")
    public CommonResult updateMqMessageLog(@RequestBody MqMessageLog entity,@ApiParam("消息id") @PathVariable Long id) {

        try {
            entity.setLogId(id);
            if (mqMessageLogService.updateById(entity)) {
                return CommonResult.success();
            }
        } catch (Exception e) {
            log.error("更新消息：{}", e.getMessage(), e);
            return CommonResult.failed();
        }
        return CommonResult.failed();
    }

    @SysLog(MODULE = "mq", REMARK = "删除消息")
    @ApiOperation("删除消息")
    @DeleteMapping(value = "/{id}")
    public CommonResult deleteMqMessageLog(@ApiParam("消息id") @PathVariable Long id) {
        try {
            if (mqMessageLogService.removeById(id)) {
                return CommonResult.success();
            }
        } catch (Exception e) {
            log.error("删除消息：{}", e.getMessage(), e);
            return CommonResult.failed();
        }
        return CommonResult.failed();
    }

    @SysLog(MODULE = "mq", REMARK = "查询消息")
    @ApiOperation("查询消息")
    @GetMapping(value = "/{id}")
    public CommonResult getMqMessageLogById(@ApiParam("消息id") @PathVariable Long id) {
        try {
            MqMessageLog coupon = mqMessageLogService.getById(id);
            return CommonResult.success(coupon);
        } catch (Exception e) {
            log.error("查询消息：{}", e.getMessage(), e);
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "批量删除消息")
    @PostMapping(value = "/delete/batch")
    @ResponseBody
    @SysLog(MODULE = "mq", REMARK = "批量消息")
    public CommonResult deleteBatch(@RequestParam("ids") List<Long> ids) {
        boolean success = mqMessageLogService.removeByIds(ids);
        return success ? CommonResult.success() : CommonResult.failed();
    }

}
